<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/22
 * Time: 17:28
 */

namespace app\api\controller;


class Logistics extends Api
{
    //我的地址
    public function getlist(){
        //获取当前用户ID
        $id = $this->cache['id'];

        //入库查询
        try{
            //根据用户ID获取该用户的所有地址信息
            $adds = model("Address")->getAddressById($id);
        }catch(\Exception $e){
            show(false,0,$e->getMessage());
        }

        if(!$adds && !is_array($adds)){
            return show(false,0,'没有数据');
        }
        $result = array();
        //拼接数据
        foreach($adds as $k => $v){
           $result[$k]['Address'] = $v['detail'];
           $result[$k]['Area'] = $v['address'];
           $result[$k]['Id'] = $v['id'];
           $result[$k]['IsDefault'] = $v['is_default'];
           $result[$k]['PhoneNumber'] = $v['phone'];
           $result[$k]['UserName'] = $v['name'];
        }

        return show(true,0,'OK',$result);
    }


    //编辑当前用户的地址
    public function geteditmodel(){
        //获取传递过来的地址ID
        $id = input('post.id');

        //判定传递过来的ID
        if(!$id && !is_numeric($id)){
            return show(false,0,'ID不能为空');
        }

        //入库查询
        try{
            //根据地址ID获取当前ID的地址信息
            $address = model("Address")->getAreaById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }


        $res = array(
            'Address' => $address['detail'],
            'Area' => $address['address'],
            'Id' => $address['id'],
            'IsDefault' => $address['is_default'],
            'PhoneNumber' => $address['phone'],
            'UserName' => $address['name'],
        );


        return show(true,0,'OK',$res);

    }


    //保存修改地址信息
    public function edit(){
        $id = input('post.Id');
        $data = array(
            'phone' => input('post.PhoneNumber'),
            'address' => input('post.Area'),
            'detail' => input('post.Address'),
            'name' => input('post.UserName'),
            'is_default' => input('post.IsDefault'),
        );

        //判定是否有ID,没有话为新增操作
        if(!$id && !is_numeric($id)){
            $data['is_default'] = 0;
        }

        //将修改的数据入库
        try{
            $res = model("Address")->editAddressById($id,$data);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        return show(true,0,'OK',$res);

    }


    //删除地址信息
    public function del(){
        //获取传递过来的ID
        $id = input('post.id');

        //判定ID是否有值
        if(!$id && !is_numeric($id)){
            return show(false,0,'ID不能为空');
        }

        //入库操作
        try{
            $res = model("Address")->DelAddressById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //判定是否删除成功并返回数据给前端
        if(!$res){
            return show(false,0,'删除失败');
        }
        return show(true,0,'',$res);

    }



    //修改默认地址
    public function setdefault(){
        //获取需要修改的地址ID
        $id = input('post.id');
        //获取用户ID
        $uid = $this->cache['id'];
        if(!$id && !is_numeric($id)){
            return show(false,0,'ID不存在');
        }

        try{
            //查询出当前用户所有的收货地址
            $addres = model("Address")->getAddressById($uid);
        }catch(\Exception $e){

        }

        //循环修改所有的地址
        foreach($addres as $k => $v){
            $aid = $v['id'];
            unset($v['id']);
            $data['is_default'] = 0;
            model("Address")->editAddressById($aid,$data);
        }

        $res = array(
            'is_default' => 1,
        );
        try{
            model("Address")->editAddressById($id,$res);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        return show(true,0,'');


    }



}